import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'

// @路径用
import path from 'path'
// 自动载入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
// elementui自动载入
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// 自动路由
import Pages from 'vite-plugin-pages'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    return {
        base: loadEnv(mode, process.cwd()).VITE_BASE,//目录
        resolve: {
            alias: {
                '@': path.resolve(__dirname, 'src')  //让文件中可以用@/代替绝对路径
            }
        },
        plugins: [
            vue(),
            AutoImport({
                resolvers: [ElementPlusResolver()],
                imports: ['vue', 'vue-router', {
                    axios: [['default', 'axios']],
                    '/src/main/func.js': ['sys', 'verify', 'func'],
                    '/src/main/bus.js': [['default', 'bus']],
                    '/src/config.js': [['default', 'config']]
                }], //自动载入的项目
            }),
            Components({
                resolvers: [ElementPlusResolver()],
                dirs: ['src/components'], //自动载入的组件
                extensions: ['vue'],
            }),
            Pages({
                pagesDir: 'src/views/', // 需要自动生成路由的文件的目录
                extensions: ['vue'], //自动路由的文件后缀
                extendRoute: (route) => {
                    //标记index.vue
                    let indexPage = false
                    if (route.component.endsWith('index.vue')) {
                        indexPage = true
                    }
                    return { ...route, indexPage: indexPage, }
                },
            }),
        ],
        build: {
            chunkSizeWarningLimit: 1000,
            rollupOptions: {
                output: {
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            const name = id.toString().split('node_modules/')[1].split('/')[0].toString() //设置文件名
                            // return name
                            if (['@vue', 'element-plus', '@wangeditor'].includes(name)) return name
                            else return 'script'
                        }
                    },
                },
            },
        },
        server: {
            host: '0.0.0.0', // 监听所有 IP
            port: 5173,       // 可指定端口
            proxy: {
                '/apiImage': {
                    target: loadEnv(mode, process.cwd()).VITE_INTERFACE_IMAGE_HTTP,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/apiImage/, '')
                },
                '/apiVideo': {
                    target: loadEnv(mode, process.cwd()).VITE_INTERFACE_VIDEO_HTTP,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/apiVideo/, '')
                },
                '/api': {
                    target: loadEnv(mode, process.cwd()).VITE_INTERFACE_HTTP,
                    changeOrigin: true,
                    rewrite: path => path.replace(/^\/api/, '')
                },
            }
        },
        css: {
            preprocessorOptions: {
                scss: {//防止scss报警告
                    api: 'modern-compiler', // or 'modern'
                }
            }
        }
    }
})
